package com.game.clases;

import com.game.main.MesaJuego;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

/**
 *
 * @author Sefer Sephirott
 */
public class Hal9000 implements MouseListener{
    
    private Player jugador;
    private Computer cpu;
    public static int totalFosforos = 22;
    private int fosforosTurno;
    private String source;
    private static MesaJuego mesaJuego;
    private JLabel message;
    private int posYFosPc = 50;
    
    public Hal9000(){
        jugador = new Player();
        cpu = new Computer();
        fosforosTurno = 0;
        message  = new JLabel();
        message.setFont(new Font("Arial", Font.BOLD, 24));
        message.setForeground(Color.red);
        message.setBounds(110, 390, 500, 100);
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MesaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MesaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MesaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MesaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                mesaJuego = new MesaJuego();
                mesaJuego.setVisible(true);
            }
        });
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        
        source = e.getSource().getClass().getSimpleName();
        
        if(source.equals("Fosforo")){
            Fosforo fosforo = (Fosforo) e.getSource();

            if(MesaJuego.turnoActual == 0){
                
                if(fosforosTurno < 2 && e.getButton() == 1){
                    int[] newPos = jugador.play();
                    fosforosTurno += 1;
                    totalFosforos -= 1;
                    fosforo.setLocation(newPos[0], newPos[1]);
                    System.out.println("Fosforos Restantes : "+totalFosforos);
                  
                }
                else if(fosforosTurno == 2 && e.getButton() == 1){          
                    
                    
                    message.setText("Ya cogiste 2 fosforos");
                    mesaJuego.getLayeredPane().add(message, JLayeredPane.PALETTE_LAYER);                    
                    
                    System.out.println("Ya cogiste 2 fosforos");
                }

                if(e.getButton() == 3){
                    MesaJuego.turnoActual = 1;
                    fosforosTurno = 0;
                    mesaJuego.getLayeredPane().remove(message);
                }
            }
            else {
                int [] newPosCpu = cpu.play(totalFosforos);
                for(int j=0; j < newPosCpu[0]; j++){
                    //mesaJuego.getLayeredPane().getComponentAt(110, 0).getComponentAt(320, 50).setLocation(newPosCpu[2], newPosCpu[3]);
                    //mesaJuego.getLayeredPane().getName();
                    System.out.println(mesaJuego.getLayeredPane().getName());
                    posYFosPc += 30;
                }
                MesaJuego.turnoActual = 0;
                totalFosforos -= newPosCpu[0];
                System.out.println("Fosforos Restantes : "+totalFosforos);
            }
        }else if(source.equals("Fosforo") || source.equals("MesaJuego") && e.getButton() == 3 ){
            MesaJuego.turnoActual = 1;
            fosforosTurno = 0;
            mesaJuego.getLayeredPane().remove(message);
        }
        
        
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
               
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
    
}
